﻿using System.Collections.Generic;
using FarseerGames.FarseerPhysics.Collisions;
using FreeGoo.GameEngine.Renderables;

namespace FreeGoo.GameEngine.Behaviors
{
    public class ResistsCollission : BodyRenderableBehavior
    {
        public ResistsCollission(BodyRenderable bodyRenderable)
            : base(bodyRenderable)
        {
            Resists = new List<BodyRenderable>();
            BodyRenderable.Geom.OnCollision += CollissionOccured;
        }

        public IList<BodyRenderable> Resists { get; private set; }

        public override void Update()
        {        
        }

        private bool CollissionOccured(Geom geometry1, Geom geometry2, ContactList contactList)
        {
            if (geometry1.Tag == null || !(geometry1.Tag is BodyRenderable) ||
                geometry2.Tag == null || !(geometry2.Tag is BodyRenderable))
            {
                return true;
            }

            BodyRenderable thisRenderable = geometry1.Tag as BodyRenderable;
            BodyRenderable otherRenderable = geometry2.Tag as BodyRenderable;

            if (BodyRenderable == otherRenderable)
            {
                otherRenderable = thisRenderable;
            }

            return !Resists.Contains(otherRenderable);
        }
    }
}
